

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.AdminBeanRemote;

/**
 * Servlet implementation class PasswordChangeServlet
 */
public class PasswordChangeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PasswordChangeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String password,userId = (String) request.getSession().getAttribute("userId");
		
		password = request.getParameter("password");
		if (password != null) {
			InitialContext context;
			try {
				context = new InitialContext();
				Object ref = context.lookup("AdminBeanRemote");
	    		AdminBeanRemote adminBean = (AdminBeanRemote)PortableRemoteObject.narrow(ref, AdminBeanRemote.class);
	    		
	    		if (adminBean.changePassword(userId, password)) {
	    			response.sendRedirect("AdminPage.jsp");
	    		}
	    		else {
	    			response.sendRedirect("PasswordChangePage.jsp");
	    		}
			} catch (NamingException e) {
				response.getWriter().println("Naming Exception");
				e.printStackTrace();
			}
		}
		else {
			response.sendRedirect("PasswordChangePage.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
